package com.dsp.services.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.core.cache.Cache;
import com.core.excel.WriterExcel;
import com.core.util.DspUtil;
import com.core.util.GenericDAO;
import com.core.util.GenericServiceHibernate;
import com.dsp.bean.Company;
import com.dsp.bean.InsureResult;
import com.dsp.bean.InsureResultsSub;
import com.dsp.dao.InsureResultDAO;
import com.dsp.services.InsureResultService;
import com.dsp.services.InsureResultsSubService;

@Service("insureResultService")
public class InsureResultServieImpl extends GenericServiceHibernate<InsureResult, Long> implements InsureResultService {


	@Autowired
	private InsureResultDAO insureResultDAO;
	
	@Resource(name = "insureResultDAO")
	public void setDao(GenericDAO<InsureResult, Serializable> dao) {
		super.setDao(dao);
	}

	/**
	 * @return the insureResultDAO
	 */
	public InsureResultDAO getInsureResultDAO() {
		return insureResultDAO;
	}

	/**
	 * @param insureResultDAO the insureResultDAO to set
	 */
	public void setInsureResultDAO(InsureResultDAO insureResultDAO) {
		this.insureResultDAO = insureResultDAO;
	}

	@Override
	public String getDownloadData(DetachedCriteria dc , String path) {
		List<InsureResult> list = super.likeBy(dc);
		List<Map<String,String>> data = new ArrayList<Map<String,String>>();
		Map<String,List<InsureResultsSub>> subData = new HashMap<String, List<InsureResultsSub>>(); 
		if (DspUtil.isEmptyList(list)) {
			return null;
		} else {
			this.getSubList(subData, list); 
		}
		Map<String,Company> company = Cache.getCompany();
		Map<String,String> map = null;
		for (InsureResult ir : list) {
			map = new HashMap<String,String>();
			map.put("yearMonth", ir.getYearMonth());
			map.put("empname", ir.getEmpName());
			map.put("hukou", ir.getHukou().toString());
			map.put("company", company.get(ir.getCompany().toString()).getCname());
			if (subData.get(ir.getId().toString()) != null){
				for(InsureResultsSub irs : subData.get(ir.getId().toString())){
					map.put(irs.getInsureType()+"_"+irs.getInsureItem()+"_Base", irs.getInsureBase().toString());
					map.put(irs.getInsureType()+"_"+irs.getInsureItem()+"_Money", irs.getInsureMoney().toString());
				}
			}
			data.add(map);
		}
		
		WriterExcel we = new WriterExcel();
		String filePath = we.running("INSURE", path, data);
		return filePath;
	}
	
	private void getSubList(Map<String,List<InsureResultsSub>> subData , List<InsureResult> data){
		List<Long> key = new ArrayList<Long>();
		for (InsureResult ir : data){
			key.add(ir.getId());
		}
		InsureResultsSubService irss = (InsureResultsSubService)DspUtil.getBean("insureResultsSubService");
		DetachedCriteria dc = DetachedCriteria.forClass(InsureResultsSub.class);
		dc.add(Restrictions.in("mainId", key));
		List<InsureResultsSub> subList = irss.likeBy(dc);
		List<InsureResultsSub> newList = null;
		if (DspUtil.isEmptyList(subList) == false) {
			for (InsureResultsSub irs : subList){
				if (subData.get(irs.getMainId().toString()) != null) {
					newList = subData.get(irs.getMainId().toString()) ;
				} else {
					newList = new ArrayList<InsureResultsSub>();
					subData.put(irs.getMainId().toString(), newList);
				}
				newList.add(irs);
			}
		}
	}
	
}
